var mysql = require('mysql')
const async = require('async')
var config = require('../config')

var pool = mysql.createPool(config.mysqlConfig)

exports.add = function (merchantName, business, addressComponents, surroundingPois, point, callback){
	pool.getConnection(function(err, connection){
		if (err) {
			console.log('Mysql getConnection Err: ' + err)
		} else {
			const task = []
			task.push(function (cbFunc) {
				var sql = `INSERT INTO merchant_address 
							(merchant_name, lng, lat, business, surroundingPois, province, city, district, street, streetNumber) 
							VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
				
				var values = []
				values.push(merchantName)
				values.push(point.lng)
				values.push(point.lat)
				values.push(business)
				values.push(JSON.stringify(surroundingPois))
				values.push(addressComponents.province)
				values.push(addressComponents.city)
				values.push(addressComponents.district)
				values.push(addressComponents.street)
				values.push(addressComponents.streetNumber)

				connection.query({sql:sql, values:values}, function(err, rows){
					if (err){
						console.log('SQL 执行异常: ' + err);
						cbFunc(new Error('数据库连接异常'))
					}else{
						console.log(rows)
						cbFunc(null, {
							ok: 'ok'
						})
					}
				})
			})
			async.waterfall(task, function (err, result) {
				connection.release();
				if (err) {
					callback({code: 0, msg: err.message, params: {}})
				} else {
					callback({
						code: 1,
						msg: 'ok',
						params: result
					})
				}
			})
		}
	});
}

exports.list = function (searchMap, callback){
	const dbKeyMap = {
		province: 'province',
		city: 'city',
		district: 'district'
	}

	pool.getConnection(function(err, connection){
		if (err) {
			console.log('Mysql getConnection Err: ' + err)
		} else {
			const task = []
			task.push(function (cbFunc) {
				var sql = `
					SELECT id, merchant_name, lng, lat, business, surroundingPois, province, city, district, street, streetNumber 
					FROM merchant_address 
					WHERE 1=1 
				`
				
				var values = []

				for (const key in searchMap) {
					const item = searchMap[key]
					if (item.searchKey && item.searchValue) {
						sql += 'AND ' + dbKeyMap[item.searchKey] + '=? '
						values.push(item.searchValue)
					}
				}

				connection.query({sql:sql, values:values}, function(err, rows){
					if (err){
						console.log('SQL 执行异常: ' + err);
						cbFunc(new Error('数据库连接异常'))
					}else{
						let list = rows;
						if (!list) {
							list = []
						}
	
						cbFunc(null, list)
					}
				})
			})
			async.waterfall(task, function (err, result) {
				connection.release();
				if (err) {
					callback({code: 0, msg: err.message, params: {}})
				} else {
					callback({
						code: 1,
						msg: 'ok',
						params: result
					})
				}
			})
		}
	});
}

exports.delete = function (id, callback){
	pool.getConnection(function(err, connection){
		if (err) {
			console.log('Mysql getConnection Err: ' + err)
		} else {
			const task = []
			task.push(function (cbFunc) {
				var sql = `DELETE FROM merchant_address WHERE id=?`
				
				var values = []
				values.push(id)

				connection.query({sql:sql, values:values}, function(err, rows){
					if (err){
						console.log('SQL 执行异常: ' + err);
						cbFunc(new Error('数据库连接异常'))
					}else{
						console.log(rows)
						cbFunc(null, {
							ok: 'ok'
						})
					}
				})
			})
			async.waterfall(task, function (err, result) {
				connection.release();
				if (err) {
					callback({code: 0, msg: err.message, params: {}})
				} else {
					callback({
						code: 1,
						msg: 'ok',
						params: result
					})
				}
			})
		}
	});
}
